package com.huawei.app.demo.video;

import android.view.SurfaceHolder;

import com.huawei.app.demo.util.LogUtils;
import com.huawei.app.httpapi.ThreadPools;

/**
 * 屏幕共享显示窗口Surface callback
 *
 * @since 2021-01-18
 */
public class ShareScreenSurfaceCallback implements SurfaceHolder.Callback {

    private static final String TAG = ShareScreenSurfaceCallback.class.getSimpleName();

    /**
     * 编解码任务
     */
    private AvcEncoderDecoderTask encoderDecoderTask;

    public ShareScreenSurfaceCallback(AvcEncoderDecoderTask encoderDecoderTask) {
        this.encoderDecoderTask = encoderDecoderTask;
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        encoderDecoderTask.initDecoder(holder.getSurface());
        ThreadPools.run(encoderDecoderTask);
        LogUtils.getInstance().i(TAG, "surfaceCreated");
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        LogUtils.getInstance().i(TAG, "surfaceChanged width=" + width + ", height=" + height);
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        if (encoderDecoderTask != null) {
            encoderDecoderTask.stop();
        }
        LogUtils.getInstance().i(TAG, "surfaceDestroyed");
    }
}
